<?php

namespace IDock\Insurance\MACN\Request;

use Exception;
use IDock\Insurance\MACN\MACNEnums;
use IDock\Insurance\MACN\Response\ClauseListResponse;
use IDock\Insurance\MACN\Response\UnderwritingResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumUnderwritingVisitor;
use IDock\Visitor\SpecialClausesVisitor;

/**
 * 特约列表
 */
class ClauseListRequest extends AllMACNRequest
{
    public $requestType = '0021';

    protected $response = ClauseListResponse::class;

    protected $urlTip = '获取特约列表';

    public $operatorCode;
    public $queryType;
    public $interSalesmanCode;
    public $queryDetail;

    public $flag;

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    /**
     * @return array
     * @throws Exception
     */
    public function generate(): array
    {
        $data = [
            'baseInfo' => [
                'askCalcNo' => $this->visitor->getExtInfoVisitor()->argc['askCalcNo'] ?? '',
                'queryType' => $this->queryType,
                'queryDetail' => $this->queryDetail,
                'flag' => $this->flag,
            ],
            'policyInfo' => $this->getPolicyInfo(),
            'holderInfo' => $this->getHolderInfo(),
            'insuredInfo' => $this->getInsuredInfo(),
            'carOwnerInfo' => $this->getCarOwnerInfo(),
            'agentComm' => $this->getAgentInfo(),
        ];


        return $data;
    }

    private function getPolicyInfo()
    {
        return [
            'companyCode' => $this->visitor->getExtInfoVisitor()->argc['companyCode'] ?? '',
            'operatorCode' =>  $this->visitor->getExtInfoVisitor()->argc['operatorCode'] ?? '',
            'electFlag' => true,
            'paperFlag' => false,
            'engageFlag' => 1,
        ];
    }

    private function getHolderInfo()
    {
        $holder = $this->visitor->getInsuredVisitor();
        $arr = [
            'personType' => $holder->typeCode == 'R1' ? '1' : '2',
            'personName' => $holder->name,
            'identifyType' => MACNEnums::$certificateTypeCode[$holder->certificateTypeCode],
            'identifyNumber' => $holder->certificateNo,
            'address' => $holder->address,
            'mobilePhone' => $holder->mobile,
            'email' => $holder->email,
            'birthDate' => $this->getBirthday($holder->certificateNo),
            'sex' => $this->getGenderByIdCard($holder->certificateNo),
            'carRelation' => '1',
            'clientLevel' => '1',
            'insuredNature' => $holder->typeCode == 'R1' ? '1' : '2',
            'insuredType' => $holder->typeCode == 'R1' ? '3' : '4',
            'unitType' => '',
            'idCardStartDate' => $holder->certificateStartDate,
            'idCardEndDate' => $holder->certificateEndDate,
        ];

        if (empty($arr['identifyType'])) {
            throw new Exception("不支持该投保人证件类型");
        }
        if ($arr['personType'] == 2) {
//            $arr['unitType'] = MACNEnums::$organizationType[$holder->organizationType] ?? '';//先注释
            $arr['unitType'] = '56';//默认其他
            if (empty($arr['unitType'])) {
                throw new Exception("不支持该投保人单位性质");
            }
        }
        return $arr;
    }

    private function getInsuredInfo()
    {
        $insured = $this->visitor->getInsuredVisitor();
        $arr = [
            'personType' => $insured->typeCode == 'R1' ? '1' : '2',
            'personName' => $insured->name,
            'identifyType' => MACNEnums::$certificateTypeCode[$insured->certificateTypeCode],
            'identifyNumber' => $insured->certificateNo,
            'address' => $insured->address,
            'mobilePhone' => $insured->mobile,
            'email' => $insured->email,
            'birthDate' => $this->getBirthday($insured->certificateNo),
            'sex' => $this->getGenderByIdCard($insured->certificateNo),
            'carRelation' => '1',
            'clientLevel' => '1',
            'insuredNature' => $insured->typeCode == 'R1' ? '1' : '2',
            'insuredType' => $insured->typeCode == 'R1' ? '3' : '4',
            'unitType' => '',
            'idCardStartDate' => $insured->certificateStartDate,
            'idCardEndDate' => $insured->certificateEndDate,
        ];

        if (empty($arr['identifyType'])) {
            throw new Exception("不支持该被保人证件类型");
        }
        if ($arr['personType'] == 2) {
//            $arr['unitType'] = MACNEnums::$organizationType[$insured->organizationType];
            $arr['unitType'] = '56';//默认其他
            if (empty($arr['unitType'])) {
                throw new Exception("不支持该被保人单位性质");
            }
        }
        return $arr;
    }

    private function getCarOwnerInfo()
    {
        $owner = $this->visitor->getOwnerVisitor();
        $arr = [
            'personType' => $owner->typeCode == 'R1' ? '1' : '2',
            'personName' => $owner->name,
            'identifyType' => MACNEnums::$certificateTypeCode[$owner->certificateTypeCode],
            'identifyNumber' => $owner->certificateNo,
            'address' => $owner->address,
            'mobilePhone' => $owner->mobile,
            'email' => $owner->email,
            'birthDate' => $this->getBirthday($owner->certificateNo),
            'sex' => $this->getGenderByIdCard($owner->certificateNo),
            'carRelation' => '1',
            'clientLevel' => '1',
            'insuredNature' => $owner->typeCode == 'R1' ? '1' : '2',
            'insuredType' => $owner->typeCode == 'R1' ? '3' : '4',
            'unitType' => '',
            'idCardStartDate' => $owner->certificateStartDate,
            'idCardEndDate' => $owner->certificateEndDate,
        ];

        if (empty($arr['identifyType'])) {
            throw new Exception("不支持该车主证件类型");
        }
        if ($arr['personType'] == 2) {
//            $arr['unitType'] = MACNEnums::$organizationType[$owner->organizationType];
            $arr['unitType'] = '56';//默认其他
            if (empty($arr['unitType'])) {
                throw new Exception("不支持该车主单位性质");
            }
        }
        return $arr;
    }

    private function getAgentInfo()
    {
        $arr = [
            'businessNature' => '',//渠道类型
            'agencyCode' => $this->interSalesmanCode,//子协议号
            'agencyName' => $this->visitor->getExtInfoVisitor()->argc['agencyName'] ?? '',//代理名称
            'agreementNO' => $this->visitor->getExtInfoVisitor()->argc['agrtNo'] ?? '',//协议号
            'crperiod' => '',//账期
            'certificateNo' => $this->visitor->getExtInfoVisitor()->argc['certificateNo'],//许可证号
            'solutionCode' => '',//分配方案
            'salePeopleName' => '',//中介销售人名字
            'salePeopleRegisterNo' => '',//中介销售人职业证号
            'partnerId' => '',//推修厂编码
            'partnerName' => '',//推修厂名称
        ];
        return $arr;
    }

    /**
     * 通过身份证获取性别
     * @param $idCard
     * @return int
     * @throws Exception
     */
    private function getGenderByIdCard($idCard)
    {
        if (strlen($idCard) != 18) {
            throw new Exception("身份证位数不合法");
        }
        $gender = intval(substr($idCard, 16, 1)) % 2;
        return $gender ? '0' : '1';
    }

    /**
     * 通过身份证获取生日
     * @param $idCard
     * @return string
     * @throws Exception
     */
    private function getBirthday($idCard)
    {
        if (strlen($idCard) != 18) {
            throw new Exception("身份证位数不合法");
        }
        return substr($idCard, 6, 4) . '-' . substr($idCard, 10, 2) . '-' . substr($idCard, 12, 2);
    }
}
